package cn.com.pansky.action.notice;

import java.util.List;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.getui.AppPush;
import cn.com.pansky.service.NoticeService;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.template.TransmissionTemplate;

public class NoticeAction extends CommonAction {
	
	 /**
	  * 查询通知
	  */
	public void getNotices() {
		
		Model user = (Model)context.getServletSession().getAttribute("user");
		String userid = user.getString("AA001");
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT a.id, a.type, a.msg, a.data, b.userid, b.isread, a.aae036 FROM ic51 a , ic52 b WHERE a.id = b.noticeid "
				+ "AND b.removed = 'N' and b.userid='"+userid+"'");
		System.out.println("aae036: "+context.getParam("AAE036"));
		if (null != context.getParam("AAE036")) {
			sql.append(" AND a.aae036 <'"+context.getParam("AAE036")+"'");
			System.out.println(sql.toString());
		}
		context.put("result", SqlHelper.page(sql.append(" ORDER BY a.aae036 DESC ").toString()));
		System.out.println(context.getDatas());
	}

	/**
	 * 获取当前时间以后的未读通知条数
	 */
	public void count() {
		Model user = (Model)context.getServletSession().getAttribute("user");
		String userid = user.getString("AA001");
		String aae036 = getParam("AAE036");
		context.put("result", SqlHelper.queryOne("sql", userid, aae036));
	}
	

	/**
	 * 标记通知为已读
	 */
	public void read() {
		String AA001 = user.getString("AA001");
		String NOTICEID = getParam("NOTICEID");
		if(context.hasError()) {
			return;
		}
		String sql = "UPDATE ic52 SET ISREAD = 'Y' WHERE  userid = ? AND noticeid = ?";
		SqlHelper.execute(sql, AA001, NOTICEID);
	}
	
	
	/**
	 * 标记通知为删除
	 */
	public void delete() {
		String AA032 = user.getString("AA032");
		Model model = SqlHelper.queryOne("select AA001 from RC10 where AA032 = ?", AA032);
		String NOTICEID = getParam("NOTICEID");
		if(context.hasError()) {
			return;
		}
		String sql = "UPDATE ic52 SET REMOVED = 'Y' WHERE  userid = ? AND noticeid = ?";
		SqlHelper.execute(sql, model.getString("AA001"),NOTICEID);
	}
	
	/**
	 * 全部推送
	 */
	public void pushToAll() {
		AppPush push = new AppPush();
		JSONObject pushData = new JSONObject();
		pushData.put("t", context.getParam("T"));//业务办理类型
		pushData.put("m", context.getParam("M"));//通知头
		pushData.put("d", "");//通知内容
		pushData.put("s", 1);//通知内容
		TransmissionTemplate template = new TransmissionTemplate();
    	APNPayload payload = new APNPayload();
    	
    	template.setTransmissionContent(pushData.toString());
    	try {
//			IPushResult result =
    			payload.setContentAvailable(0);
    			APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
    			alertMsg.setBody(context.getParam("M"));
    			alertMsg.setLocKey(context.getParam("T"));
    			payload.setAlertMsg(alertMsg);
    			template.setAPNInfo(payload);
				push.push(template, payload);
				NoticeService notice = new NoticeService();
				pushData.put("c", 1);
				notice.add(pushData, "");
		} catch (Exception e) {
			e.printStackTrace();
			context.putError(-1, "调用第三方接口失败，请联系管理员！");
		}
	}
	/**
	 * 单个推送
	 */
	public void pushToSingle() {
		AppPush push = new AppPush();
		TransmissionTemplate template = new TransmissionTemplate();
		APNPayload payload = new APNPayload();
		payload.setContentAvailable(0);
		APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
		alertMsg.setBody(context.getParam("M"));
		alertMsg.setLocKey(context.getParam("T"));
		payload.setAlertMsg(alertMsg);
		template.setAPNInfo(payload);
		JSONObject pushData = new JSONObject();
		pushData.put("t", context.getParam("T"));//通知标题
		pushData.put("m", context.getParam("M"));//通知头
		pushData.put("d", "");//通知内容
		pushData.put("s", 1);//通知类型
		List<Model> list = SqlHelper.query("sql");
		if(list.size()>0) {
			for (Model model : list) {
				String alias = model.getString("AA001");
				try {
					// 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
					if(AppPush.getUserStatus(alias)) {
						template.setTransmissionContent(pushData.toString());
					}else{
						payload.addCustomMsg("t", pushData.get("t"));
						payload.addCustomMsg("m", pushData.get("m"));
						payload.addCustomMsg("d", pushData.get("d"));
						template.setAPNInfo(payload);
					}
					push.pushtoSingle(template, alias, payload);
					NoticeService notice = new NoticeService();
					pushData.put("c", 2);
					notice.add(pushData, alias);
				} catch (Exception e) {
					e.printStackTrace();
					context.putError(-1, "调用第三方接口失败，请联系管理员");
				}
			}
		}
		
	}
}

